package com.kingdee.pda.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.kingdee.pda.R;

import java.io.File;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Glide图片加载工具类
 * Created by zhanlg
 * DATE 2018/3/26
 * TIME 10:51
 */

public class GlideUtils {


    public static void loadImage(Context context, String url, ImageView imageView) {
        if (!isContextValid(context)) {
            return;
        }
        if (TextUtils.isEmpty(url)) {
            imageView.setImageResource(R.mipmap.image_pic_default);
            return;
        }
        Glide.with(context)
                .load(url)
                .error(R.mipmap.image_pic_default)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .fitCenter()
                .dontAnimate()
                .dontTransform()
                .priority(Priority.NORMAL)
                .into(imageView);
    }


    public static void loadImageNoDefault(Context context, String url, ImageView imageView) {
        if (!isContextValid(context) || TextUtils.isEmpty(url)) {
            return;
        }
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .fitCenter()
                .dontAnimate()
                .dontTransform()
                .into(imageView);
    }

    public static void loadImageCenterCrop(Context context, String url, ImageView imageView) {
        if (!isContextValid(context) || TextUtils.isEmpty(url)) {
            return;
        }
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .dontAnimate()
                .dontTransform()
                .priority(Priority.LOW)
                .into(imageView);
    }

    /**
     * 加载列表内图片
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadListImage(Context context, String url, ImageView imageView) {
        if (!isContextValid(context)) {
            return;
        }
        Glide.with(context)
                .load(url)
//                .placeholder(R.mipmap.icon_goods_default)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .fitCenter()
                .error(R.mipmap.image_pic_default)
                .priority(Priority.LOW)
                .into(imageView);
    }


    /**
     * 加载资源图片
     * @param resId
     * @param imageView
     */
    public static void loadResImage(Context context, int resId, ImageView imageView) {
        if (!isContextValid(context)) {
            return;
        }
        Glide.with(context)
                .load(resId)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .fitCenter()
                .dontAnimate()
                .into(imageView);
    }

    /**
     * 判断Context对象是否可用
     * @param context
     * @return
     */
    public static boolean isContextValid(Context context) {
        if (context == null) {
            return false;
        }
        if (context instanceof Activity) {
            Activity activity = ((Activity) context);
            if (activity.isDestroyed()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 加载本地小程序码
     * @param path
     * @param imageView
     */
    public static void loadLocalQrcode(Context context, String path, ImageView imageView) {
        if (!isContextValid(context)) {
            return;
        }
        Glide.with(context)
                .load(path)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .dontAnimate()
                .dontTransform()
                .fitCenter()
                .into(imageView);
    }

    public static void loadLocalQrcode(Context context, File file, ImageView imageView) {
        if (!isContextValid(context) && file.length() > 0) {
            return;
        }
        Glide.with(context)
                .load(file)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .dontAnimate()
                .dontTransform()
                .fitCenter()
                .priority(Priority.HIGH)
                .into(imageView);
    }

    public static void loadFullScreenImage(Context context, File file, ImageView imageView) {
        if (!isContextValid(context)) {
            return;
        }
        Glide.with(context)
                .load(file)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .override(ScreenUtils.getScreenWidth(context), ScreenUtils.getScreenHeight(context))
                .into(imageView);
    }




    public static Bitmap getBitmap(String url) {
        URL imageURL = null;
        Bitmap bitmap = null;
        try {
            imageURL = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = null;
            if (imageURL != null) {
                conn = (HttpURLConnection) imageURL
                        .openConnection();
            }
            if (conn == null) {
                return null;
            }
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();

        } catch (NullPointerException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

}
